package com.mega.fe.util;

import com.mega.fe.Unravel;
import com.mega.fe.common.ModifyShader;
import com.mega.fe.handler.EventHandler;
import com.mega.fe.render.shaders.ShaderInit;
import com.mega.fe.util.bus.MegaEventBus;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.DeathScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.eventbus.BusBuilderImpl;
import net.minecraftforge.eventbus.api.IEventBus;

import java.util.HashSet;

public class BaseInit {
    public static HashSet<Commands> commands_instances = new HashSet<>();
    public static HashSet<Entity> entity_instances = new HashSet<>();
    public static HashSet<String> Gods = new HashSet<>();
    public static HashSet<Long> Death = new HashSet<>();
    public static HashSet<String> DeathUUID = new HashSet<>();
    static IEventBus EVENT_BUS = new MegaEventBus((BusBuilderImpl) new BusBuilderImpl().startShutdown().useModLauncher());

    public static IEventBus EVENT_BUS() {
        return EVENT_BUS;
    }

    public static void replaceBus() {
    }

    public static void addGod(Entity entity) {
        Gods.add(entity.getStringUUID());
    }

    public static boolean isGod(Object o) {
        if (o == null)
            return false;
        if (!(o instanceof Entity entity))
            return false;
        return Gods.contains(entity.getStringUUID());
    }

    public static void addDeath(Entity entity) {
        try {
            Death.add(UnsafeUtil.toAddress(entity));
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        DeathUUID.add(entity.getStringUUID());
        entity.getPersistentData().putBoolean("fe_dea", true);
    }

    public static boolean isDeath(Object o) {
        if (!(o instanceof Entity entity))
            return false;
        if (isGod(o))
            return false;
        if (o == null)
            return false;
        if (DeathUUID.contains(entity.getStringUUID()))
            return true;
        try {
            return Death.contains(UnsafeUtil.toAddress(entity));
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean clientGod() {
        return isGod(Minecraft.getInstance().player);
    }

    public static void busRegister() {
        EVENT_BUS.register(Unravel.class);
        EVENT_BUS.register(EventHandler.class);
        EVENT_BUS.register(EventHandler.Client.class);
        EVENT_BUS.register(EventHandler.Server.class);
        EVENT_BUS.register(ModifyShader.class);
        EVENT_BUS.register(ShaderInit.class);
        Unravel.out(EVENT_BUS.getClass());
    }

    public static boolean mayDeathScreen(Screen screen) {
        if (screen == null)
            return false;
        if (screen.getTitle().contains(Component.translatable("deathScreen.title")) || screen.getTitle().contains(Component.translatable("deathScreen.title.hardcore")))
            return true;
        if (!(screen.getClass().getName().startsWith("net.minecraft") || screen.getClass().getName().startsWith("com.mega"))
                && (screen.getClass().getSimpleName().contains("Death")
                || screen.getClass().getSimpleName().contains("death")
                || screen.getClass().getSimpleName().contains("Dead")
                || screen.getClass().getSimpleName().contains("dead")
                || screen.getClass().getSimpleName().contains("si")
                || screen.getClass().getSimpleName().contains("Die")
                || screen.getClass().getSimpleName().contains("die"))) {
            return true;
        }
        return screen instanceof DeathScreen;
    }
}
